<?php
namespace Shop\Controller;
use Common\Controller\AdminbaseController;
use Think\Image;
class AttachmentController extends AdminbaseController{
    
    protected $model;
    
    function _initialize(){
        parent::_initialize();
        $this->model = D('attachment');
    }
    public function index(){
        $data = $this->model->select();
        foreach ($data as &$temp){
            $temp['isset'] = file_exists("./uploads".$temp['savepath'].$temp['savename']);
            $temp['s_isset'] = file_exists("./uploads".$temp['savepath']."s_".$temp['savename']);
            $temp['m_isset'] = file_exists("./uploads".$temp['savepath']."m_".$temp['savename']);
            $temp['l_isset'] = file_exists("./uploads".$temp['savepath']."l_".$temp['savename']);
            $temp['is_use'] = D('shop')->where(array("attachmentID"=>$temp['id']))->count();
        }
        $this->assign("attachments",$data);
        
        $this->display();
    }
    
    
    public function makeup(){
        if(I('get.id')){
            $attachment = $this->model->find(I('get.id'));
            $path = "./uploads".$attachment['savepath'];
            $file = $path.$attachment['savename'];
            $img = new Image();
            $img->open($file);
            $img->thumb(700,700)->save($path."l_".$attachment['savename']);
            $img->thumb(400,400)->save($path."m_".$attachment['savename']);
            $img->thumb(200,200)->save($path."s_".$attachment['savename']);
            $this->success("生成成功！",U('attachment/index'));
        }
        if (I('post.ids')) {
            $ids = I('post.ids');
            explode(",",$ids);
            foreach ($ids as $id){
                $attachment = $this->model->find($id);
                $path = "./uploads".$attachment['savepath'];
                $file = $path.$attachment['savename'];
                $img = new Image();
                $img->open($file);
                $img->thumb(700,700)->save($path."l_".$attachment['savename']);
                $img->thumb(400,400)->save($path."m_".$attachment['savename']);
                $img->thumb(200,200)->save($path."s_".$attachment['savename']);
            }
            $this->success("生成成功！",U('attachment/index'));
        }
    }
    
    public function delete(){
        if(I('get.id')){
            $attachment = $this->model->find(I('get.id'));
            unlink("./uploads".$attachment['savepath'].$attachment['savename']);
            unlink("./uploads".$attachment['savepath']."l_".$attachment['savename']);
            unlink("./uploads".$attachment['savepath']."m_".$attachment['savename']);
            unlink("./uploads".$attachment['savepath']."s_".$attachment['savename']);
            $this->model->delete(I('get.id'));
        }
        if(I("post.ids")){
            $ids = I("post.ids");
            explode(",",$ids);
            foreach ($ids as $id){
                $attachment = $this->model->find($id);
                unlink("./uploads".$attachment['savepath'].$attachment['savename']);
                unlink("./uploads".$attachment['savepath']."l_".$attachment['savename']);
                unlink("./uploads".$attachment['savepath']."m_".$attachment['savename']);
                unlink("./uploads".$attachment['savepath']."s_".$attachment['savename']);
                $this->model->delete($id);
            }
        }
        $this->success("删除成功！");
    }
}